﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;
using MsDAL;
using System.ComponentModel;
using Components;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Bas1a40Manager : Bas1a40Base
    {
        private static Bas1a40Manager _entity;

        public static Bas1a40Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Bas1a40Manager();
                return _entity;
            }
        }

        #region 个人代码
        public string SaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();

            Bas1a40 model = null;
            Bas1a41 dmodel = null;
            if (dtl == null || dtl.Rows.Count == 0 && "A" == action) return "明细中没有数据，不需要保存！";

            string appstr = Context.GetAppByUserCode(usercode);

            if ("A" == action)
            {
                model = new Bas1a40();
                CommonUtil.GetInstance.CopyProperties(tb, model); //复制
                if (string.IsNullOrEmpty(model.Cntcode)) //合同单号未填写，自动生成
                {
                    model.Cntcode = Sys0a60Manager.Entity.CreateSn("bas1a40_docno", usercode);
                }

                model.Muser = appstr;
                model.Listid = Sys0a60Manager.Entity.CreateSn("bas1a40_listid", usercode); //生成合同表主键

                lst.Add(this.Add(model)); // 保存概要信息
            }
            else if ("M" == action)
            {
                model = this.GetModelById(Validation.ToString(tb["listid"]));

                CommonUtil.GetInstance.CopyProperties(tb, model); //复制

                lst.Add(this.Update(model)); //修改
            }

            if (null != dtl)
            {
                foreach (DataRow dr in dtl.Rows)
                {
                    dmodel = new Bas1a41();
                    dmodel.Plistid = model.Listid;

                    if (Validation.IsNull(dr["listid"]))
                    {
                        dmodel.Listid = Sys0a60Manager.Entity.CreateSn("bas1a41_listid", usercode);
                        dmodel.Taxid = Validation.ToString(dr["taxid"]);
                        dmodel.Taxpct = Validation.ToDouble(dr["taxpct"]);
                        dmodel.Taxapp = Validation.ToString(dr["taxapp"]);

                        lst.Add(Bas1a41Manager.Entity.Add(dmodel)); //保存明细表
                    }
                    else
                    {
                        CommonUtil.GetInstance.CopyProperties(dmodel, dr); //复制
                        lst.Add(Bas1a41Manager.Entity.Update(dmodel));
                    }
                }
            }

            if ("M" == action && null != deldata)
            {
                foreach (DataRow dr in deldata.Rows)
                {
                    dmodel = new Bas1a41();
                    dmodel.Listid = Validation.ToString(dr["listid"]);
                    lst.Add(this.Delete(model));
                }
            }

            return CommonManager.Entity.SaveData(lst);
        }

        #endregion

    }
}



